//
//  ENPdetail.m
//  企业信息查询
//
//  Created by 梁中豪 on 16/4/20.
//  Copyright © 2016年 梁中豪. All rights reserved.
//

#import "ENPdetail.h"
#import <BmobSDK/Bmob.h>

@interface ENPdetail ()

- (IBAction)shoucang:(id)sender;
- (IBAction)callnum:(id)sender;

@end

@implementation ENPdetail

- (void)viewDidLoad {
    [super viewDidLoad];
    _detailenp.text = _detailenpname;
     [_detailcall setTitle:_detailcallname forState:UIControlStateNormal];
    _detailadress.text = _detailadressname;
    _detailmail.text = _detailmailname;
    _lawman.text = _lawmanname;
    _setmoney.text = _setmoneyname;
    _setdate.text = _setdatename;
    
    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)shoucang:(id)sender {
    
    NSString *tep;
    BmobUser *tempuser =[BmobUser getCurrentUser];
    if (tempuser) {
        tep = [tempuser objectForKey:@"username"];
        
        BmobObject  *addata = [BmobObject objectWithClassName:tep];
        [addata setObject:_detailenp.text forKey:@"Ename"];
        [addata setObject:_detailmail.text forKey:@"email"];
        [addata setObject:_detailcallname forKey:@"call"];
        [addata setObject:_detailadress.text forKey:@"adress"];
        [addata setObject:_lawman.text forKey:@"lawman"];
        [addata setObject:_setdate.text forKey:@"setdate"];
        [addata setObject:_setmoney.text forKey:@"setmoney"];

        
        //异步保存到服务器
        [addata saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
            if (isSuccessful) {
                UIAlertController *shoucang = [UIAlertController
                                               alertControllerWithTitle:@"成功"
                                               message:@"已添加到您的关注中"
                                               preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                                    style:UIAlertActionStyleCancel
                                                                  handler:^(UIAlertAction *action){
                                                                  }];
                [shoucang addAction:yesAction];
                //显示
                [self presentViewController:shoucang animated:true completion:nil];
            } else if (error){
                //发生错误后的动作
                NSLog(@"%@",error);
            } else {
                NSLog(@"Unknow error");
            }
        }];
    }
    else{
        UIAlertController *noshoucang = [UIAlertController
                                       alertControllerWithTitle:@"添加失败"
                                       message:@"请登录后使用收藏功能"
                                       preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"确定"
                                                            style:UIAlertActionStyleCancel
                                                          handler:^(UIAlertAction *action){
                                                          }];
        [noshoucang addAction:yesAction];
        //显示
        [self presentViewController:noshoucang animated:true completion:nil];
    }
    
    

}

- (IBAction)callnum:(id)sender {
    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",_detailcallname];
    UIWebView * callWebview = [[UIWebView alloc] init];
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    [self.view addSubview:callWebview];
}
@end
